package ui.components;

import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.geom.Rectangle2D;

/**
 * User: [J0k3r]
 * Date: 15.02.2009
 * Time: 17:26:10
 */
public class JJMultilineCellRenderer extends JTextArea
        implements TableCellRenderer
{
    private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
    public JJMultilineCellRenderer() {
        setLineWrap(true);
        setWrapStyleWord(true);
        setOpaque(true);
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected, boolean hasFocus, int row, int column) {
        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }
//        setFont(adaptee.getFont());
        
        if (hasFocus) {
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
            if (table.isCellEditable(row, column)) {
                setForeground(UIManager.getColor("Table.focusCellForeground"));
                setBackground(UIManager.getColor("Table.focusCellBackground"));
            }
        } else {
            setBorder(new EmptyBorder(1, 2, 1, 2));
        }
        setText((value == null) ? "" : value.toString());
        if (value != null) {
            int rowCnt = 1;
            String s = value.toString();
            for (int nI = 0; nI < s.length(); ++nI)
            if (s.charAt(nI) == '\n') rowCnt++;
            int height = rowCnt * adaptee.getGraphics().getFont().getSize() + (rowCnt - 1) * adaptee.getFont().getSize() / 3;
            if (table.getRowHeight() < height) table.setRowHeight(row, height);
        }

        return this;
    }
}